<template>
  <div class="flex flex-col h-full">
    <SearchContainer is-width @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="dateRange" label="手术日期" class="w-[320px]">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="createDateRange" label="创建日期" class="w-[320px] mr-2">
          <a-range-picker v-model:value="formState.createDateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="zjm" class="w-[300px] mr-2">
          <a-input v-model:value="formState.zjm" placeholder="姓名/助记码/卡号/手机号/身份证号" />
        </a-form-item>
        <a-form-item label="手术名称" class="w-[350px] mr-1" name="ssdm">
          <a-select
            v-model:value="formState.ssdm"
            placeholder="请选择"
            show-search
            allow-clear
            :options="surgeryOptionList"
            :filter-option="false"
            @search="getSurgery"
          />
        </a-form-item>
        <a-form-item label="手术医生" class="!w-64" name="aptmOprEmpId">
          <SelectDoctor v-model:value="formState.aptmOprEmpId" :search-type="true" />
        </a-form-item>
        <a-form-item label="咨询师" class="!w-64 ml-5" name="brZxs">
          <a-select
            v-model:value="formState.brZxs"
            placeholder="请选择"
            show-search
            allow-clear
            :filter-option="false"
            :options="Counselors"
            @search="getCounselors"
          />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div>
        <a-button type="primary" class="mb-2 mr-2" @click="onAdd()"> 新增 </a-button>
        <BaseExport type="手术" name="手术预约" code="000036" :search-params="formState">导出</BaseExport>
      </div>
      <BaseTable :loading="loading" column-code="000036" fill :scroll="{ x: 1900 }" :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex == 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex == 'statusStr'">
            <a-tag v-if="record.status === '0'" color="blue">未登记</a-tag>
            <a-tag v-else-if="record.status === '1'" color="green">已登记</a-tag>
            <a-tag v-else-if="record.status === '2'" color="green">已手术</a-tag>
            <a-tag v-else color="red">已作废</a-tag>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" :disabled="record.status !== '0'" @click="onEdit(record)"> 修改 </a-button>
            <a-divider type="vertical" />
            <BaseButton confirm-text="是否作废?" :disabled="record.status !== '0'" type="text" danger size="small" @click="onCancel(record)"> 作废 </BaseButton>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="onSuceess" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'
import Edit from './Edit'

const surgeryOptionList = ref([])
const rowData = ref({})
const editOpen = ref(false)
const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().add(7, 'day').format('YYYY-MM-DD')],
  // createDateRange: [dayjs().format('YYYY-MM-DD'), dayjs().add(7, 'day').format('YYYY-MM-DD')],
  createDateRange: [],
  status: ''
})
const Counselors = ref([])

const onEdit = async (row) => {
  rowData.value = row
  editOpen.value = true
}
const onAdd = () => {
  rowData.value = {}
  editOpen.value = true
}
const getCounselors = async (mcbm = '') => {
  const { code, data } = await post('/operation/opex/getEmpInfoList', {
    yxzt: 0,
    mcbm,
    emplbid: '14',
    pageNum: 1,
    pageSize: 9999
  })
  if (code !== 0) return
  Counselors.value = data.map((item) => {
    return {
      label: item.empName,
      value: item.empId
    }
  })
}
//手术检索

const getSurgery = async () => {
  const { code, data } = await post('/common/masterData/getCanUseDrugList', {
    xmlbList: [6]
  })
  if (code !== 0) return
  surgeryOptionList.value = data.map((item) => {
    return {
      label: item.mc,
      value: item.bh
    }
  })
}
const { loading, tableData, pagination, getData } = useTable({
  url: '/operation/opex/getAppointmentRecordList',
  immediate: true,
  params: formState.value,
  formatParams: (params) => {
    return {
      ...params,
      stdate: params.dateRange?.[0],
      eddate: params.dateRange?.[1],
      createTimeStdate: params.createDateRange?.[0],
      createTimeEddate: params.createDateRange?.[1]
    }
  }
})
const onCancel = async (record) => {
  const res = await post('/operation/opex/doUpdateAppointmentStatus', record)
  if (res.code !== 0) return message.error(res.msg)
  getData()
}
const onSuceess = () => {
  editOpen.value = false
  getData()
}
onMounted(() => {
  getSurgery()
  getCounselors()
})
</script>
